﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheoriesBraun.DAL;

namespace TheoriesBraun
{
    public partial class ChooseChapter : Form
    {
        TextBox shareTextbox;

        public ChooseChapter()
        {
            InitializeComponent();
            loadCbxTheories();
        }

        public ChooseChapter(TextBox TBoxForm1)
        {
            InitializeComponent();

            this.shareTextbox = TBoxForm1;
            loadCbxTheories();
        }

        public void loadCbxTheories()
        {
            TheoriesDAL tdal = new TheoriesDAL();
            DataView dv = new DataView(tdal.loadTheoryList());
            clbChapter.DataSource = dv;
            clbChapter.DisplayMember = "Chapter";
            clbChapter.ValueMember = "ChapterAndName";
        }

        private void clbChapter_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (clbChapter.CheckedItems.Count > 1)
                {
                    throw new System.Exception();
                }
                else
                {
                    string text = clbChapter.SelectedValue.ToString();
                    string[] chapter = text.Split('-');
                    txtSelected.Text = chapter[0];
                }
            }
            catch (Exception)
            {
                MessageBox.Show("You can't choose more than one chapter!");
                clbChapter.SetItemChecked(clbChapter.SelectedIndex, false);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            shareTextbox.Text = txtSelected.Text;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
